package guilayer;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.border.TitledBorder;

import controllayer.SupplierCtr;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import modellayer.Supplier;

import javax.swing.UIManager;

public class NewSupplierPanel extends JPanel {
	private JPanel viewSupplierPanel;
	
	private SupplierCtr sCtr;
	private JTextField txtName;
	private JButton btnCreateSupplier;
	private JTextField txtAddress;
	private JTextField txtCountry;
	private JTextField txtPhoneNo;
	private JLabel lblDeliveryStatus;
	private JLabel lblDeliveryDate;
	private JLabel lblCustomer;
	private JTextField txtEmail;
	private JLabel lblEmail;

	/**
	 * Create the panel.
	 * @throws SQLException 
	 */
	public NewSupplierPanel() throws SQLException {
		sCtr = new SupplierCtr();
		initPanel();
		initComponents();
		
	}

	private void initPanel() {
		setLayout(new BorderLayout(0, 0));
		
		viewSupplierPanel = new JPanel();
		viewSupplierPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "New supplier", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(viewSupplierPanel, BorderLayout.CENTER);
		viewSupplierPanel.setLayout(null);
		
		
	}

	private void initComponents() throws SQLException {
		
		JLabel lblDate = new JLabel("Name:");
		lblDate.setBounds(10, 27, 110, 14);
		viewSupplierPanel.add(lblDate);
		
		txtName = new JTextField();
		txtName.setColumns(10);
		txtName.setBounds(130, 24, 214, 20);
		viewSupplierPanel.add(txtName);
		
		txtAddress = new JTextField();
		txtAddress.setColumns(10);
		txtAddress.setBounds(130, 55, 214, 20);
		viewSupplierPanel.add(txtAddress);
		
		txtCountry = new JTextField();
		txtCountry.setColumns(10);
		txtCountry.setBounds(130, 86, 214, 20);
		viewSupplierPanel.add(txtCountry);
		
		txtPhoneNo = new JTextField();
		txtPhoneNo.setColumns(10);
		txtPhoneNo.setBounds(130, 117, 214, 20);
		viewSupplierPanel.add(txtPhoneNo);
		
		lblDeliveryStatus = new JLabel("Address:");
		lblDeliveryStatus.setBounds(10, 58, 110, 14);
		viewSupplierPanel.add(lblDeliveryStatus);
		
		lblDeliveryDate = new JLabel("Country:");
		lblDeliveryDate.setBounds(10, 89, 110, 14);
		viewSupplierPanel.add(lblDeliveryDate);
		
		lblCustomer = new JLabel("Phone number:");
		lblCustomer.setBounds(10, 120, 110, 14);
		viewSupplierPanel.add(lblCustomer);
		
		btnCreateSupplier = new JButton("Create supplier");
		btnCreateSupplier.setBounds(223, 179, 121, 23);
		btnCreateSupplier.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
					createSupplier();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }
    });
		viewSupplierPanel.add(btnCreateSupplier);
		
		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(130, 148, 214, 20);
		viewSupplierPanel.add(txtEmail);
		
		lblEmail = new JLabel("Email:");
		lblEmail.setBounds(10, 151, 110, 14);
		viewSupplierPanel.add(lblEmail);
	}
	
	public void createSupplier() throws SQLException {
		SupplierPanel supplierPanel = SupplierPanel.getInstance();
		String name = txtName.getText();
		String address = txtAddress.getText();
		String country = txtCountry.getText();
		String phoneNo = txtPhoneNo.getText();
		String email = txtEmail.getText();
		
		if(!name.isEmpty() && !address.isEmpty() && !country.isEmpty() && !phoneNo.isEmpty() && !email.isEmpty()) {
			sCtr.createSupplier(name, address, country, phoneNo, email);
			supplierPanel.updateTable();
		}else {
			JOptionPane.showMessageDialog(viewSupplierPanel, "Error", "Error Message", JOptionPane.ERROR_MESSAGE);
		}
	}
}
